import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/person/userBase'

const api = {

    /**
     * 新增员工基本信息
     * @param userBase
     * @returns {Promise<*|undefined>}
     */
    createUserBase: (userBase) =>
      axiosClient().post(`${requestUrl}/addInfo`, userBase),

    /**
     * 根据ID删除员工基本信息
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteUserBaseById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新员工基本信息
     * @param id
     * @param userBase
     * @returns {Promise<*|undefined>}
     */
    updateUserBaseFull: (id, userBase) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, userBase),

    /**
     * 根据ID部分更新员工基本信息
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateUserBasePartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询员工基本信息
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getUserBaseByCode: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询员工基本信息
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getUserBasePage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询员工基本信息
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getUserBaseList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.userNum) {
            params.append('userNum', businessParam.userNum)
        }
        if (businessParam.userCode) {
            params.append('userCode', businessParam.userCode)
        }
        if (businessParam.userName) {
            params.append('userName', businessParam.userName)
        }
        if (businessParam.sex) {
            params.append('sex', businessParam.sex)
        }
        if (businessParam.email) {
            params.append('email', businessParam.email)
        }
        if (businessParam.entryDate) {
            params.append('entryDate', businessParam.entryDate)
        }
        if (businessParam.workPhone) {
            params.append('workPhone', businessParam.workPhone)
        }
        if (businessParam.mobile) {
            params.append('mobile', businessParam.mobile)
        }
        if (businessParam.identityType) {
            params.append('identityType', businessParam.identityType)
        }
        if (businessParam.identityNo) {
            params.append('identityNo', businessParam.identityNo)
        }
        if (businessParam.birthday) {
            params.append('birthday', businessParam.birthday)
        }
        if (businessParam.qq) {
            params.append('qq', businessParam.qq)
        }
        if (businessParam.weChat) {
            params.append('weChat', businessParam.weChat)
        }
        if (businessParam.weChatId) {
            params.append('weChatId', businessParam.weChatId)
        }
        if (businessParam.userStatus) {
            params.append('userStatus', businessParam.userStatus)
        }
        if (businessParam.employmentType) {
            params.append('employmentType', businessParam.employmentType)
        }
        if (businessParam.emergencyName) {
            params.append('emergencyName', businessParam.emergencyName)
        }
        if (businessParam.emergencyMobile) {
            params.append('emergencyMobile', businessParam.emergencyMobile)
        }
        if (businessParam.education) {
            params.append('education', businessParam.education)
        }
        if (businessParam.contractStartDate) {
            params.append('contractStartDate', businessParam.contractStartDate)
        }
        if (businessParam.contractEndDate) {
            params.append('contractEndDate', businessParam.contractEndDate)
        }
        if (businessParam.tenantId) {
            params.append('tenantId', businessParam.tenantId)
        }
        if (businessParam.applicationId) {
            params.append('applicationId', businessParam.applicationId)
        }
        if (businessParam.createUserId) {
            params.append('createUserId', businessParam.createUserId)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
